package OnlineExam.question;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import OnlineExam.question.*;
import OnlineExam.db.*;
import java.util.*;



/**
 * Question bean 
 */

public class Question extends HibernateQuestion 
{

	private String[] optionsArr;
	public String[] getOptionsArr ()
	{
		return optionsArr;
	}
	public void setOptionsArr (String[] optionsArr)
	{
		this.optionsArr = optionsArr;
	}
	/** 
	 * Input validation.
	 */
	
		
	public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) 
	{

		servlet.log ("Question : " + getQuestion());
    	servlet.log("Options:" + getOptions());      
    	servlet.log("CategoryAdmin : " + getCorrectAnswer());      

		ActionErrors errors = new ActionErrors();
    
		if (getQuestion() == null || getQuestion().equals("")) 
		{
      		errors.add("Question ", new ActionMessage("error.question"));
    	}

	
		/*else if (options == null || options.equals("")) 
		{
      		errors.add("Options", new ActionMessage("error.options"));
    	}*/

  		else if (getCorrectAnswer() == null || getCorrectAnswer().equals("")) 
		{
      		errors.add("Correct Answer", new ActionMessage("error.correctAnswer"));
    	}
		else if (isQuestionPresent(getQuestion(),getCategoryId()))
		{
			errors.add("Question Present", new ActionMessage("error.questionNotUnique"));
		}
		System.out.println ("error set:" + errors);
    	return errors;	
  	}

	/**
	 *  Checks if the Question is already present in the database.
	 */


	private static boolean isQuestionPresent (String question, String categoryId)
    {
		Session session = null;
		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
       		session = sessionFactory.openSession();
		
			Query query = session.createQuery("from QuestionBank in class OnlineExam.question.Question where Question = :question and CategoryId = :categoryId");  
			query.setString("question", question);
			query.setString("categoryId", categoryId);

			List list = query.list();
			
			return (list.size() > 0);
		}
		catch (Exception e)
		{
			System.out.println("Exception.." + e);
			return false;
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception.." + e);}
		}
	}
	

	/**
	 *  retrieveQuestion: returns the Question object for a given the question number.
	 */

  	public static Question retrieveQuestion (int dbQuestionNumber) 
  	{
			
		try
		{
			List list = DBUtil.select ("QuestionBank", "OnlineExam.question.Question" , "Number", dbQuestionNumber);
			
			// assert list size to be 1
			return (Question) list.listIterator().next();
		}
		catch (DBException e)
		{
			System.out.println ("Exception..." + e);
			return null;
		}
	}

	
	/**
	 *  Splits the options on a particular separator and returns them as an array of Strings.
	 */

	public String[] splitOptions ()
	{
		String answers[] = getOptions().split(";;");  // ";;" is the separator being used.
		return answers;
	}

	/**
	 *  Concatenates the options on a particular separator and returns the concatenated String.
	 */

	public static String assembleOptions (String options[])
	{
		String optionsString = "";

		for (int i = 0; i < options.length; i++)
		{
			optionsString = optionsString.concat(options[i]);
			optionsString = optionsString.concat(";;"); //";;" is the separator being used.
		}
		
		return optionsString;
	}

	/**
	 *  Checks if a questions needs an Explanation returns false if it does not need an explanation.
	 */

	public boolean needsExplanation()
	{
		return (!(getExplanation().equals("N.A")));  // null == Explanation not needed.
	}	
}
